package com.cedifie.lendapp.ui.steps.fm

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cedifie.lendapp.R
import com.cedifie.lendapp.databinding.FmStepOcrBinding
import com.cedifie.lendapp.ui.steps.adapter.CedOcrTypeAdapter
import com.cedifie.lendapp.utils.decoration.LinearSpacingItemDecoration
import com.lib.base.base.BaseFragment
import com.lib.base.utils.DensityUtils
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime:  2024/6/18 14:46
 * @Annotation：流程 - 证件照
 */
class CedStepOcrFm(): BaseFragment() {
    private var _binding: FmStepOcrBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "binding is null"
    )

    //类型列表
    private val typeIconList = arrayListOf(R.mipmap.icon_ocr_idcard,R.mipmap.icon_ocr_passport,R.mipmap.icon_ocr_drving_license,R.mipmap.icon_ocr_election_card)
    private val typeTitleList = arrayListOf(R.string.process_ocr_idcard,R.string.process_ocr_passport,R.string.process_ocr_drving_license,R.string.process_ocr_election_card)

    //身份验证类型
    private var typeAdapter: CedOcrTypeAdapter? = null
    override fun getCedRootView(inflater: LayoutInflater): View {
        _binding = FmStepOcrBinding.inflate(inflater)
        return binding.root
    }

    override fun initCedView() {
        rootViewBinding.containerLayout.setBackgroundColor(ContextCompat.getColor(requireContext(),R.color.transparent))
        initCedRecyclerView()
    }

    override fun initCedListener() {
    }

    override fun initCedData() {
        val typeList = arrayListOf<JSONObject>()
        for (i in 0..<typeIconList.size){
            val obj = JSONObject()
            obj.put("isSelect",i == 0)
            obj.put("iconId",typeIconList[i])
            obj.put("title",typeTitleList[i])
            typeList.add(obj)
        }
        typeAdapter?.data = typeList
        showContentView(0)
    }

    /**
     * 初始化Ocr类型列表
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun initCedRecyclerView() {
        _binding?.recyclerView?.setHasFixedSize(true)
        _binding?.recyclerView?.isNestedScrollingEnabled = false
        _binding?.recyclerView?.isFocusableInTouchMode = false //不需要焦点
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = RecyclerView.HORIZONTAL
        _binding?.recyclerView?.layoutManager = layoutManager
        typeAdapter = CedOcrTypeAdapter(_binding?.recyclerView)
        val topDecoration = DensityUtils.dp2px(12f)
        _binding?.recyclerView?.addItemDecoration(LinearSpacingItemDecoration(0,topDecoration,0,0))
        _binding?.recyclerView?.adapter = typeAdapter
        _binding?.recyclerView?.itemAnimator = DefaultItemAnimator()
        typeAdapter?.setOnItemChildClickListener { _, childView, position ->
            if (childView.id == R.id.itemLayout) {
                typeAdapter?.let {
                    for (i in 0..< it.itemCount) {
                        val item = it.getItem(i)
                        if (i == position) {
                            item.put("isSelect", true)
                        } else {
                            item.put("isSelect", false)
                        }
                    }
                    it.notifyDataSetChanged()
                    showContentView(position)
                }
            }
        }
    }

    /**
     * 切换类型，显示内容
     */
    @SuppressLint("CommitTransaction")
    private fun showContentView(index: Int){
        _binding?.textOcrType?.text = getString(typeTitleList[index])
        when(index){
            0 -> {//身份证信息
                childFragmentManager
                    .beginTransaction()
                    .replace(R.id.contentLayout, CedOcrIDCardFm("IDCARD")) // 此处的R.id.fragment_container是要盛放fragment的父容器
                    .commitAllowingStateLoss()
            }
            1 -> {//护照信息
                childFragmentManager
                    .beginTransaction()
                    .replace(R.id.contentLayout, CedOcrPassportFm()) // 此处的R.id.fragment_container是要盛放fragment的父容器
                    .commitAllowingStateLoss()
            }
            2 -> {//驾驶证信息
                childFragmentManager
                    .beginTransaction()
                    .replace(R.id.contentLayout, CedOcrIDCardFm("DRIVING")) // 此处的R.id.fragment_container是要盛放fragment的父容器
                    .commitAllowingStateLoss()
            }
            else -> {//选举卡
                childFragmentManager
                    .beginTransaction()
                    .replace(R.id.contentLayout, CedOcrIDCardFm("ELECTIONCARD")) // 此处的R.id.fragment_container是要盛放fragment的父容器
                    .commitAllowingStateLoss()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }
}